package org.androbuzz;

import android.R.string;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;

public class AndrobuzzSettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
	
	private static final String TAG = "AndroBuzz.Settings";
	private SharedPreferences.Editor preferenceEditor = null;
	private SharedPreferences Application;
	public String LoginName;
	private String currentAccount;
	
	final AndroBuzzDbAdapter mDb = new AndroBuzzDbAdapter(this);
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.preferenceEditor  = PreferenceManager.getDefaultSharedPreferences(this).edit();
		this.Application = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		//this.updatePreferences();
		addPreferencesFromResource(R.layout.settings);

    	mDb.open();
    	mDb.findProfilesByName();
    	//mDb.findCurrentProfileId();
    	// Get current account
    	currentAccount = this.Application.getString("AndroBuzzLoginId", "").toLowerCase();
    	Log.d(TAG, "Get current account: " + currentAccount);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();

		mDb.close();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}
	
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String Key) {
			if (Key.equals("AndroBuzzLoginId")) {
				String newAccount = sharedPreferences.getString("AndroBuzzLoginId", null).toLowerCase();
				if (currentAccount == null){
					mDb.addProfile(newAccount, true);
				}
				else if (newAccount.length() == 0){
					Toast.makeText(this, "Account cannot be empty", Toast.LENGTH_SHORT).show();
				}
				else if (currentAccount.equals(newAccount) == false) {
					try {
						if (currentAccount.length() > 0) {
							mDb.unsetDefaultProfile(currentAccount);
						}
						if(!mDb.findProfilesByName().contains(newAccount) && newAccount.length() > 0){
							mDb.addProfile(newAccount, true);
							// The switch will be handle by PreferenceManager
						}
						else {
							mDb.setDefaultProfile(newAccount);
						}
					}
					catch (Exception ex) {
						Toast.makeText(this, "Unable to save account", Toast.LENGTH_SHORT).show();
					}
					Toast.makeText(this, "Account changed to " + newAccount, Toast.LENGTH_SHORT).show();
				}
				currentAccount = newAccount;
			}
	}
}
